import { FormControl, ValidationErrors } from '@angular/forms';
import { IdCard } from '../../../utils/idCard';

export function IdCardValidator(control: FormControl): ValidationErrors {
  let result = true;
  result = control.value && (control.value.length === 18 || control.value.length === 15);
  if (result) {
    result = IdCard(control.value);
  }
  return result ? null : { 'idCard': true };
}

export function mobilePhoneValidator(control: FormControl): ValidationErrors {
  return !control.value || /^1([356789][0-9]|4[579]|66|7[0135678]|9[189])[0-9]{8}$/.test(control.value) ? null : { 'mobilePhone': true };
}

export function licensePlateValidator(control: FormControl): ValidationErrors {
  let result = false;
  result = control.value && (control.value.length === 7 || control.value.length === 8);
  if (result) {
    result = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([ABCDEFGHJK]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[ABCDEFGHJK]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$/.test(control.value);
  }
  return result ? null : { 'licensePlate': true };
}
